package com.jzqf.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;

/**
 * 多边形背景 textview
 * 2020/5/6 18:21
 *
 * @author LiuWeiHao
 * Email 872235631@qq.com
 */
public class RhombusTextView extends androidx.appcompat.widget.AppCompatTextView {
    private Paint mPaintBg;
    private Path mPath;

    public RhombusTextView(Context context) {
        super(context);
        init();
    }

    public RhombusTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RhombusTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaintBg = new Paint();
        mPaintBg.setStyle(Paint.Style.FILL);
        mPaintBg.setColor(Color.WHITE);
        mPath = new Path();
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mPath.moveTo(getPaddingLeft(), 1);
        mPath.lineTo(getWidth(), 1);
        mPath.lineTo(getWidth() - getPaddingRight(), getHeight());
        mPath.lineTo(1, getHeight());
        mPath.close();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(mPath, mPaintBg);
        super.onDraw(canvas);
    }
}
